<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Finance;

use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;
/**
 * 贷款收款
 * Class Loan
 *
 * @author Xmh
 */
class Loan
{
    private $LoanLogModel;
    private $CarModel;
    private $Addr;

    public function __construct()
    {
        $this->LoanLogModel = M('loan_log');
        $this->CarModel = new CarModel();
        $this->Addr = new Address();
    }

    /**
     * 获取贷款收款列表 -- 过户完成
     * @param int $page
     * @param int $size
     * @param     $params
     *
     * @return array
     */
    public function get_loan_list($page = 1, $size = 10, $params){
        $page = (int) $page < 1 ? 1 : (int) $page;
        $size = (int) $size < 1 ? 10 : (int) $size;
        $map = array();
        if($params['status'] == 2){
            $map['ll.loan_status'] = ['eq',2];
        }elseif($params['status'] == 3){
            $map['ll.loan_status'] = ['eq',3];
        }elseif($params['status'] == 4){
            $map['ll.loan_status'] = ['eq',4];
        }
        if (!empty($params['location_area'])) {
            $map['c.location_area'] = ['eq',$params['location_area']];
        }
        if(empty($params['location_area']) && $params['bc_city']){
            $map['c.location_area'] = ['in',$params['bc_city']];
        }
        if($params['loan_no']){
            $map['ll.xm_list_code'] = ['LIKE', '%'.$params['loan_no'].'%'];
        }
        if($params['order_no']){
            $map['o.order_no'] = ['LIKE', '%'.$params['order_no'].'%'];
        }
        if($params['contact_person']){
            $map['cd.contact_person'] = ['LIKE', '%'. $params['contact_person'] . '%'];
        }
        if($params['contact_mobile']){
            $map['cd.contact_mobile'] = ['LIKE', '%'. $params['contact_mobile'] . '%'];
        }
        if($params['status'] == 2){ //过户时间
            if ($params['start_time']) {
                $map['l.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
            }
            if ($params['end_time']) {
                $map['l.createtime'] = array('elt', $params['end_time'] . ' 23:59:59');
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['l.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            }
        }elseif($params['status'] == 3){ //放款时间
            if ($params['start_time']) {
                $map['ll.loan_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            }
            if ($params['end_time']) {
                $map['ll.loan_time'] = array('elt', $params['end_time'] . ' 23:59:59');
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['ll.loan_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            }
        }elseif($params['status'] == 4){ //收款时间
            if ($params['start_time']) {
                $map['ll.verify_loan_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            }
            if ($params['end_time']) {
                $map['ll.verify_loan_time'] = array('elt', $params['end_time'] . ' 23:59:59');
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['ll.verify_loan_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            }
        }
        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,
                 c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,
                 c.brand_id,c.series_id,c.model_id,pl.pay_time,
                 cd.contact_person,cd.contact_mobile,l.createtime as transfer_success_time,
                 ll.id as xm_id,ll.xm_list_code,ll.loan_money,ll.loan_status,ll.loan_time,ll.verify_loan_time,
                 ll.createtime as loan_createtime";
        $prefix = C('DB_PREFIX');
        $order = 'll.id DESC,o.order_id DESC';
        $total = $this->LoanLogModel->alias('ll')
            ->join($prefix . "order as o ON o.order_id = ll.order_id")
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join($prefix . "transfer_log as l ON l.order_id = o.order_id AND l.result = 2")
            ->join("LEFT JOIN " . $prefix . "proceeds_log as pl ON pl.order_id = o.order_id")
            ->where($map)->count();
        $list =  $this->LoanLogModel->alias('ll')->field($cols)
            ->join($prefix . "order as o ON o.order_id = ll.order_id")
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join($prefix . "transfer_log as l ON l.order_id = o.order_id AND l.result = 2")
            ->join("LEFT JOIN " . $prefix . "proceeds_log as pl ON pl.order_id = o.order_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
//        echo M()->_sql();

        if(!empty($list)){
            foreach($list as $key=>$val){
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['location_area_name'] = $this->Addr->get_name_by_code($val['location_area']);
                $brand = $this->CarModel->get_model_by_id($val['brand_id'], 'brand');
                $series = $this->CarModel->get_model_by_id($val['series_id'], 'series');
                $model = $this->CarModel->get_model_by_id($val['model_id']);
                $list[$key]['car_no'] = $val['car_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['transfer_time'] = $val['transfer_success_time']>0?$val['transfer_success_time']:'--';
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取贷款收款列表 -- 未过户完成
     * @param int $page
     * @param int $size
     * @param     $params
     *
     * @return array
     */
    public function get_notransfer_loan_list($page = 1, $size = 10, $params){
        $page = (int) $page < 1 ? 1 : (int) $page;
        $size = (int) $size < 1 ? 10 : (int) $size;
        $map = array();
        if($params['status'] == 1){
            $map['ll.loan_status'] = ['eq',1];
            $map['o.status'] = ['lt',11];
        }
        if (!empty($params['location_area'])) {
            $map['c.location_area'] = ['eq',$params['location_area']];
        }
        if(empty($params['location_area']) && $params['bc_city']){
            $map['c.location_area'] = ['in',$params['bc_city']];
        }
        if($params['loan_no']){
            $map['ll.xm_list_code'] = ['LIKE', '%'.$params['loan_no'].'%'];
        }
        if($params['order_no']){
            $map['o.order_no'] = ['LIKE', '%'.$params['order_no'].'%'];
        }
        if($params['contact_person']){
            $map['cd.contact_person'] = ['LIKE', '%'. $params['contact_person'] . '%'];
        }
        if($params['contact_mobile']){
            $map['cd.contact_mobile'] = ['LIKE', '%'. $params['contact_mobile'] . '%'];
        }
        if ($params['start_time']) {
            $map['ll.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['ll.createtime'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['ll.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }

        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,
                 c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,
                 c.brand_id,c.series_id,c.model_id,pl.pay_type,pl.pay_time,pl.verify_time,
                 cd.contact_person,cd.contact_mobile,
                 ll.id as xm_id,ll.xm_list_code,ll.loan_money,ll.loan_status,ll.loan_time,ll.verify_loan_time,
                 ll.createtime as loan_createtime";
        $prefix = C('DB_PREFIX');
        $order = 'll.id DESC,o.order_id DESC';
        $total = $this->LoanLogModel->alias('ll')
            ->join($prefix . "order as o ON o.order_id = ll.order_id")
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join("LEFT JOIN " . $prefix . "proceeds_log as pl ON pl.order_id = o.order_id")
            ->where($map)->count();
        $list =  $this->LoanLogModel->alias('ll')->field($cols)
            ->join($prefix . "order as o ON o.order_id = ll.order_id")
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join("LEFT JOIN " . $prefix . "proceeds_log as pl ON pl.order_id = o.order_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
//        echo M()->_sql();

        if(!empty($list)){
            foreach($list as $key=>$val){
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['location_area_name'] = $this->Addr->get_name_by_code($val['location_area']);
                $brand = $this->CarModel->get_model_by_id($val['brand_id'], 'brand');
                $series = $this->CarModel->get_model_by_id($val['series_id'], 'series');
                $model = $this->CarModel->get_model_by_id($val['model_id']);
                $list[$key]['car_no'] = $val['car_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['transfer_time'] = $val['transfer_success_time']>0 ? $val['transfer_success_time'] : '--';
                if($val['pay_type'] == 3){
                    $pay_time = $val['verify_time'];
                }elseif($val['pay_type'] == 1 || $val['pay_type'] == 2){
                    $pay_time = $val['pay_time'];
                }
                $list[$key]['pay_time'] = $pay_time > 0 ? $pay_time : '--';
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }



    /**
     * 获得贷款收款详情
     * @param $xm_id
     */
    public function get_loan_detail_by_xm_id($xm_id){
        if($xm_id < 1){
            return false;
        }
        $condition['id'] = ['eq',$xm_id];
        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,
                 c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,
                 c.brand_id,c.series_id,c.model_id,cd.contact_person,cd.contact_mobile,
                 ll.id as xm_id,ll.xm_list_code,ll.loan_money,ll.loan_status,ll.createtime as loan_createtime";
        $prefix = C('DB_PREFIX');
        $info =  $this->LoanLogModel->alias('ll')->field($cols)
            ->join($prefix . "order as o ON o.order_id = ll.order_id")
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->where($condition)->find();
        $info['location_area_name'] = $this->Addr->get_name_by_code($info['location_area']);
        $brand = $this->CarModel->get_model_by_id($info['brand_id'], 'brand');
        $series = $this->CarModel->get_model_by_id($info['series_id'], 'series');
        $model = $this->CarModel->get_model_by_id($info['model_id']);
        $info['car_model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        return $info;
    }

    /**
     * 确认放款
     * @param $xm_id
     */
    public function confirm_loan_verify($xm_id){
        if($xm_id < 1){
            return false;
        }
        $condition['id'] = ['eq', $xm_id];
        $data['loan_status'] = 4;
        $data['verify_loan_time'] = date("Y-m-d H:i:s",time());
        return $this->LoanLogModel->where($condition)->save($data);
    }

    /**
     * 获得贷款收款详情
     * @param $order_id
     * @return bool|mixed
     */
    public function get_loan_detail($order_id){
        if($order_id < 1){
            return false;
        }
        $cols = 'xm_list_code,loan_money,first_pay_money,loan_status,createtime';
        $condition['order_id'] = ['eq', $order_id];
        return $this->LoanLogModel->field($cols)->where($condition)->find();
    }
}